﻿using System;
using System.Collections.Generic;
using System.IO;
using ProjScan.Config;
using ProjScan.ScanRule.CustomRules;
using ProjScan.Submodules.Custom_Rules_Check;
using ProjScan.Tools;
using UnityEngine;

namespace ProjScan.Modules
{
	// Token: 0x02000307 RID: 775
	internal class CustomRulesCheckModule : BaseModules<CustomRulesCheckModule, CustomRulesCheckConfig>
	{
		// Token: 0x0600124F RID: 4687 RVA: 0x000088C0 File Offset: 0x00006AC0
		public List<Type> GetRuleTypes()
		{
			return this.ruleTypes;
		}

		// Token: 0x06001250 RID: 4688 RVA: 0x000554E4 File Offset: 0x000536E4
		private static string FormatRuleIdentity(ICustomRule _param0, RuleDataTable _param1)
		{
			string str = _param0.Id.ToString();
			return (_param1 != null) ? (str + _param1.GetColumnGUID()) : (str + "000000");
		}

		// Token: 0x06001251 RID: 4689 RVA: 0x00055524 File Offset: 0x00053724
		private static void WriteTableToStream(ref StreamWriter _param0, RuleDataTable _param1)
		{
			_param0.WriteLine(_param1.GetTitle());
			_param0.WriteLine(_param1.GetColTypes());
			for (int i = 0; i < _param1.RowCount; i++)
			{
				_param0.WriteLine(_param1.GetRow(i));
			}
		}

		// Token: 0x06001252 RID: 4690 RVA: 0x000088C8 File Offset: 0x00006AC8
		public override void Init()
		{
			this.config = (Singleton<ConfigMgr>.Instance.GetConfig<CustomRulesCheckConfig>() as CustomRulesCheckConfig);
			this.ruleTypes = this.config.ruleTypes;
			this.ruleTypeDictionary = this.config.ruleTypeDictionary;
		}

		// Token: 0x06001253 RID: 4691 RVA: 0x00055574 File Offset: 0x00053774
		public override void Run(StreamWriter _param1)
		{
			Singleton<PerformanceMeasurer>.Instance.PushState("Custom_Rules_Check");
			ScanLog.Instance.LogInfo(Module.Custom_Rules_Check);
			ToolsHelper.ShowProgressBar("Custom Rules", "Init", 0f, false);
			this.Init();
			bool flag = this.ruleTypeDictionary.Count == 0;
			if (flag)
			{
				Debug.Log("未检测到自定义规则类，自定义规则未运行。");
				this.Close();
			}
			else
			{
				int num = 4000;
				StreamWriter streamWriter = Singleton<FileStreamFactory>.Instance.GetStreamWriter(ToolsHelper.GetScanOutPut() + "/customrules.csv");
				streamWriter.WriteLine("RuleName,Priority,LocalId,Result,DataId");
				int num2 = 0;
				Singleton<PerformanceMeasurer>.Instance.StartStateExecutionMeasure("CustomRule");
				foreach (KeyValuePair<ushort, ICustomRule> keyValuePair in this.ruleTypeDictionary)
				{
					ToolsHelper.ShowProgressBar("Custom Rules", string.Format("Rule: {0}({1}/{2})", keyValuePair.Value.GetType(), num2, this.ruleTypeDictionary.Count), (float)num2 * 1f / (float)this.ruleTypeDictionary.Count, false);
					RuleDataTable param = null;
					bool flag2 = false;
					bool? flag3 = null;
					try
					{
						flag3 = new bool?(keyValuePair.Value.Run(out flag2, out param));
					}
					catch (InterruptEx interruptEx)
					{
						ScanLog instance = ScanLog.Instance;
						ScanLog.LogLevel logLevel = ScanLog.LogLevel.EXCEPTION;
						Module module = Module.Custom_Rules_Check;
						ScanLog.Stage stage = ScanLog.Stage.s3;
						string name = keyValuePair.Value.GetType().Name;
						string[] array = new string[6];
						array[0] = "自定义规则类异常终止：";
						int num3 = 1;
						Type type = interruptEx.GetType();
						array[num3] = ((type != null) ? type.ToString() : null);
						array[2] = "\n";
						array[3] = interruptEx.Message;
						array[4] = "\n";
						array[5] = interruptEx.StackTrace;
						instance.LogInfo(logLevel, module, stage, name, string.Concat(array));
						Debug.LogError(string.Concat(new string[]
						{
							"自定义规则类：",
							keyValuePair.Value.GetType().Name,
							"\n",
							interruptEx.Message,
							"\n",
							interruptEx.StackTrace
						}));
					}
					catch (Exception ex)
					{
						ScanLog instance2 = ScanLog.Instance;
						ScanLog.LogLevel logLevel2 = ScanLog.LogLevel.EXCEPTION;
						Module module2 = Module.Custom_Rules_Check;
						ScanLog.Stage stage2 = ScanLog.Stage.s3;
						string name2 = keyValuePair.Value.GetType().Name;
						string[] array2 = new string[6];
						array2[0] = "自定义规则类异常终止：";
						int num4 = 1;
						Type type2 = ex.GetType();
						array2[num4] = ((type2 != null) ? type2.ToString() : null);
						array2[2] = "\n";
						array2[3] = ex.Message;
						array2[4] = "\n";
						array2[5] = ex.StackTrace;
						instance2.LogInfo(logLevel2, module2, stage2, name2, string.Concat(array2));
						Debug.LogError(string.Concat(new string[]
						{
							"自定义规则类：",
							keyValuePair.Value.GetType().Name,
							"\n",
							ex.Message,
							"\n",
							ex.StackTrace
						}));
					}
					bool flag4 = flag3 != null;
					if (flag4)
					{
						bool flag5 = flag2;
						if (flag5)
						{
							streamWriter.WriteLine(string.Concat(new string[]
							{
								ToolsHelper.QuoteString(keyValuePair.Value.Description),
								",",
								keyValuePair.Value.Priority.ToString(),
								",",
								CustomRulesCheckModule.FormatRuleIdentity(keyValuePair.Value, param),
								",",
								flag3.Value.ToString(),
								",",
								num.ToString()
							}));
							string filePath = ToolsHelper.GetScanOutPut() + "/custom.data." + num.ToString() + ".csv";
							StreamWriter streamWriter2 = Singleton<FileStreamFactory>.Instance.GetStreamWriter(filePath);
							CustomRulesCheckModule.WriteTableToStream(ref streamWriter2, param);
							streamWriter2.Close();
							num++;
						}
						else
						{
							streamWriter.WriteLine(string.Concat(new string[]
							{
								ToolsHelper.QuoteString(keyValuePair.Value.Description),
								",",
								keyValuePair.Value.Priority.ToString(),
								",",
								CustomRulesCheckModule.FormatRuleIdentity(keyValuePair.Value, null),
								",",
								flag3.Value.ToString(),
								","
							}));
						}
					}
					else
					{
						streamWriter.WriteLine(ToolsHelper.QuoteString(keyValuePair.Value.Description) + "," + keyValuePair.Value.Priority.ToString() + ",,,");
					}
				}
				Singleton<PerformanceMeasurer>.Instance.StopStateExecutionMeasure((long)this.ruleTypeDictionary.Count);
				streamWriter.Flush();
				streamWriter.Close();
				this.Close();
			}
		}

		// Token: 0x06001254 RID: 4692 RVA: 0x00008902 File Offset: 0x00006B02
		public override void Close()
		{
			Singleton<StackTraceLogger>.Instance.OutputAndClearLog();
			Singleton<PerformanceMeasurer>.Instance.CloseAndRecordState();
			Singleton<PerformanceMeasurer>.Instance.AddExecutionCheckpoint("CustomRulesCheckEnd");
			ScanLog.Instance.LogInfo(Module.Custom_Rules_Check, ScanLog.Stage.s6, "Done.");
		}

		// Token: 0x04000AAF RID: 2735
		private List<Type> ruleTypes;

		// Token: 0x04000AB0 RID: 2736
		private Dictionary<ushort, ICustomRule> ruleTypeDictionary;

		// Token: 0x04000AB1 RID: 2737
		protected Type tempAttribute;
	}
}
